package com.slipper.common.bo;

import java.beans.PropertyDescriptor;
import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.slipper.util.DateUtil;
import com.slipper.util.Tool;

import com.slipper.core.gson.GsonExclude;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

public abstract class CommonBO implements Serializable {
    /** 页索引 */
    @GsonExclude
    private int pageNumber;

    /** 每页行数 */
    @GsonExclude
    private int pageSize;

    /** 查询开始时间 */
    @GsonExclude
    private Long startDate;

    /** 查询结束时间 */
    @GsonExclude
    private Long endDate;

    /** 排序字段 */
    @GsonExclude
    private String sortField;

    /** 排序类型 */
    @GsonExclude
    private String sortType;

    /** 当前登录人ID */
    @GsonExclude
    private String currEmpId;

    /** 当前登录人ID */
    @GsonExclude
    private String currEmpName;

    /** 当前登录人部门ID */
    @GsonExclude
    private String currOrgId;

    /** 当前登录人单位ID */
    @GsonExclude
    private String currUnitId;

    /** 当前登录人单位ID */
    @GsonExclude
    private String operateId;

    public int getPageNumber() {
        return pageNumber <= 0 ? 1 : pageNumber;
    }

    public void setPageNumber(int pageNumber) {
        this.pageNumber = pageNumber;
    }

    public int getPageSize() {
        return pageSize <= 0 ? 10 : pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public Long getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate != null ? startDate.getTime() : null;
    }

    public void setStartDate(String startDate) throws Exception {
        this.startDate = Tool.isNotBlank(startDate) ? DateUtil.parseDate(startDate).getTime() : null;
    }

    public void setStartDate(Long startDate) {
        this.startDate = startDate;
    }

    public Long getEndDate() {
        return endDate != null ? endDate + 24 * 60 * 60 * 1000 : null;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate != null ? endDate.getTime() : null;
    }

    public void setEndDate(String endDate) throws Exception {
        this.endDate = Tool.isNotBlank(endDate) ? DateUtil.parseDate(endDate).getTime() : null;
    }

    public void setEndDate(Long endDate) {
        this.endDate = endDate;
    }

    public String getSortField() {
        if (sortField == null || sortField.isEmpty()) {
            return sortField;
        }
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < sortField.length(); i++) {
            if (Character.isUpperCase(sortField.charAt(i))) {
                builder.append("_").append(Character.toLowerCase(sortField.charAt(i)));
            } else {
                builder.append(sortField.charAt(i));
            }
        }
        return builder.toString();
    }

    public void setSortField(String sortField) {
        this.sortField = sortField;
    }

    public String getSortType() {
        return sortType;
    }

    public void setSortType(String sortType) {
        this.sortType = sortType;
    }

    public String getCurrEmpId() {
        return currEmpId;
    }

    public void setCurrEmpId(String currEmpId) {
        this.currEmpId = currEmpId;
    }

    public String getCurrEmpName() {
        return currEmpName;
    }

    public void setCurrEmpName(String currEmpName) {
        this.currEmpName = currEmpName;
    }

    public String getCurrOrgId() {
        return currOrgId;
    }

    public void setCurrOrgId(String currOrgId) {
        this.currOrgId = currOrgId;
    }

    public String getCurrUnitId() {
        return currUnitId;
    }

    public void setCurrUnitId(String currUnitId) {
        this.currUnitId = currUnitId;
    }

    public void setDateRange(Date[] dateRange) {
        setStartDate(dateRange == null ? null : dateRange.length > 0 ? dateRange[0] : null);
        setEndDate(dateRange == null ? null : dateRange.length > 1 ? dateRange[1] : null);
    }

    // 用于like查询
    private Map<String, Object> resultMap = null;
    public Map<String, Object> getLike() {
        if (resultMap != null) return resultMap;

        resultMap = new HashMap<>();
        BeanWrapper wrapper = new BeanWrapperImpl(this);
        for (PropertyDescriptor item : wrapper.getPropertyDescriptors()) {
            if (item.getPropertyType().isAssignableFrom(String.class)
                    && Tool.isNotBlank(wrapper.getPropertyValue(item.getName()))) {
                resultMap.put(item.getName(), "%" + wrapper.getPropertyValue(item.getName()) + "%");
            }
        }

        return resultMap;
    }

    public void setUpdateId(String updateId) {}
    public String getUpdateId() { return null; }
    public void setCreateId(String createId) {}
    public String getCreateId() { return null; }

    public void setOperateId(String operateId) {
        this.operateId = operateId;
        this.setUpdateId(operateId);
        this.setCreateId(operateId);
    }

    public String getOperateId() {
        return this.operateId == null
                ? getUpdateId() == null ? getCreateId() : getUpdateId()
                : this.operateId;
    }
}
